package com.gy.zpchat.ui.friend;

import static android.widget.LinearLayout.VERTICAL;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;

import im.zego.zim.ZIM;
import im.zego.zim.callback.ZIMFriendApplicationAcceptedCallback;
import im.zego.zim.callback.ZIMFriendApplicationListQueriedCallback;
import im.zego.zim.callback.ZIMFriendApplicationRejectedCallback;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.entity.ZIMFriendApplicationAcceptConfig;
import im.zego.zim.entity.ZIMFriendApplicationInfo;
import im.zego.zim.entity.ZIMFriendApplicationListQueryConfig;
import im.zego.zim.entity.ZIMFriendApplicationRejectConfig;
import im.zego.zim.entity.ZIMFriendInfo;
import im.zego.zim.entity.ZIMUserInfo;
import com.gy.zpchat.MyApplication;
import com.gy.zpchat.R;
import com.gy.zpchat.SDKManager;
import com.gy.zpchat.ui.friend.adapter.FriendRequestListAdapter;
import com.gy.zpchat.util.AppLogger;

import java.util.ArrayList;
import java.util.HashMap;

public class ZIMFriendRequestListActivity extends Activity {

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, ZIMFriendRequestListActivity.class);
        context.startActivity(intent);
    }

    FriendRequestListAdapter friendRequestListAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friend_request_list);

        RecyclerView conversationRecyclerView = findViewById(R.id.friend_request_list);

        LinearLayoutManager linearLayoutManager =
            new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
        conversationRecyclerView.setLayoutManager(linearLayoutManager);
        friendRequestListAdapter = new FriendRequestListAdapter();
        conversationRecyclerView.setItemAnimator(new DefaultItemAnimator());
        conversationRecyclerView.setAdapter(friendRequestListAdapter);

        ZIMFriendApplicationListQueryConfig config = new ZIMFriendApplicationListQueryConfig();
        config.count = 3000;

        friendRequestListAdapter.setOnItemClickListener(
            new FriendRequestListAdapter.OnItemClickListener() {
                @Override
                public void onItemAcceptClick(ZIMFriendApplicationInfo friendApplicationInfo) {


                    EditText friendAttributeKey = new EditText(MyApplication.getCurrentActivity());
                    EditText friendAttributeValue = new EditText(MyApplication.getCurrentActivity());
                    EditText alias = new EditText(MyApplication.getCurrentActivity());
                    friendAttributeKey.setHint("input friend attribute key");
                    friendAttributeValue.setHint("input friend attribute value");
                    alias.setHint("input friend alias");
                    LinearLayout layout = new LinearLayout(MyApplication.getCurrentActivity());
                    layout.setOrientation( VERTICAL);
                    layout.addView(friendAttributeKey);
                    layout.addView(friendAttributeValue);
                    layout.addView(alias);
                    new AlertDialog.Builder(MyApplication.getCurrentActivity())
                            .setTitle("Update Friend Info")
                            .setView(layout)
                            .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    String key = friendAttributeKey.getText().toString();
                                    String value = friendAttributeValue.getText().toString();
                                    String aliasS = alias.getText().toString();
                                    ZIMFriendApplicationAcceptConfig config =
                                            new ZIMFriendApplicationAcceptConfig();
                                    config.friendAlias = aliasS;
                                    config.friendAttributes = new HashMap<>();
                                    config.friendAttributes.put(key, value);
                                    ZIM.getInstance().acceptFriendApplication(
                                            friendApplicationInfo.applyUser.userID, config,
                                            new ZIMFriendApplicationAcceptedCallback() {
                                                @Override
                                                public void onFriendApplicationAccepted(ZIMFriendInfo friendInfo, ZIMError errorInfo) {

                                                    AppLogger.getInstance().d("onFriendApplicationAccepted friendInfoList: %s zimError %s",
                                                            new Gson().toJson(friendApplicationInfo),  new Gson().toJson(errorInfo));
                                                    Toast
                                                            .makeText(ZIMFriendRequestListActivity.this, errorInfo.message,
                                                                    Toast.LENGTH_LONG)
                                                            .show();
                                                    ZIMFriendApplicationListQueryConfig config2 = new ZIMFriendApplicationListQueryConfig();
                                                    config2.count = 3000;
                                                    ZIM.getInstance().queryFriendApplicationList(
                                                            config2, ZIMFriendRequestListActivity.this.callback );
                                                }
                                            });
                                    dialog.dismiss();
                                }
                            })
                            .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            })
                            .show();



                }

                @Override
                public void onItemRefuseClick(ZIMFriendApplicationInfo friendApplicationInfo) {
                    ZIMFriendApplicationRejectConfig config =
                        new ZIMFriendApplicationRejectConfig();

                    ZIM.getInstance().rejectFriendApplication(
                        friendApplicationInfo.applyUser.userID, config,
                        new ZIMFriendApplicationRejectedCallback() {
                            @Override
                            public void onFriendApplicationRejected(ZIMUserInfo zimUserInfo,
                                                                    ZIMError zimError) {
                                AppLogger.getInstance().d("onFriendApplicationRejected zimUserInfo: %s zimError %s",
                                        new Gson().toJson(zimUserInfo),  new Gson().toJson(zimError));
                                Toast
                                    .makeText(ZIMFriendRequestListActivity.this, zimError.message,
                                              Toast.LENGTH_LONG)
                                    .show();

                                ZIMFriendApplicationListQueryConfig config2 = new ZIMFriendApplicationListQueryConfig();
                                config2.count = 3000;
                                ZIM.getInstance().queryFriendApplicationList(
                                        config2, ZIMFriendRequestListActivity.this.callback );
                            }
                        });
                }
            });

        ZIM.getInstance().queryFriendApplicationList(
            config, callback );

        SDKManager.sharedInstance().setFriendApplicationEvent(
            new SDKManager.OnFriendApplicationEventCallBack() {
                @Override
                public void onFriendApplicationInfoUpdated(
                    ZIM zim, ArrayList<ZIMFriendApplicationInfo> friendApplicationInfos) {
                    friendRequestListAdapter.update(friendApplicationInfos);
                }

                @Override
                public void onFriendApplicationListAdded(
                    ZIM zim, ArrayList<ZIMFriendApplicationInfo> friendApplicationInfos) {
                    friendRequestListAdapter.add(friendApplicationInfos);
                }
            });
    }

    ZIMFriendApplicationListQueriedCallback callback = new ZIMFriendApplicationListQueriedCallback() {
        @Override
        public void onFriendApplicationListQueried(
                ArrayList<ZIMFriendApplicationInfo> infoArrayList, int nextFlag,
        ZIMError zimError) {

            AppLogger.getInstance().d(
                    "onFriendApplicationListQueried infoArrayList: %s",
                    new Gson().toJson(infoArrayList));
            friendRequestListAdapter.addAll(infoArrayList);


        }
    };
}
